// 04myCenter/03receiveAddress/03enterpriseAddress/01changeEnterpriseAddress/changeEnterpriseAddress.js
var co = require('../../../../resource/js/common.js')
var map = require('../../../../resource/js/map.js')
var app = getApp()
var _this, _data, _opt, lat = {} //经纬度
Page({
   data: {
      bizDetail: '',
      name: '', //显示的名字
      tel: '', //显示的电话
      wd: '', //纬度
      js: '', //经度
      inprovince: '', //显示的省份
      incity: '', //显示城市
      indistrict: '', //显示地区(县)
      addressCode: '',//省市区编码
      workerList: '',//员工列表
      reserveName: '',//备用联系人
      reservePhone: '',//备用联系人电话
      workerPop: false,//备用联系人弹窗
      Receiver_ID: '',//备用人ID
   },

   onLoad: function (options) {
      _this = this
      _data = this.data
      _opt = options
      this.getEnterpriseAddress()//获取地址详情
      this.getWorkerList()//获取员工列表
      //console.log(_opt)
   },

   //获取企业地址详情
   getEnterpriseAddress: function (biz) {
      var url = 'jyhs/Admin/getInfo'
      co.getShareApi(url, {
         Biz_ID: _opt.Biz_ID
      }, function (res) {
         //console.log('res==', res)
         if (res.code == 0) {
            _this.setData({
               bizDetail: res.data,
               name: res.data.Biz_Contact,
               tel: res.data.Biz_Phone,
               reserveName: res.data.Receiver_Name,
               reservePhone: res.data.Receiver_Mobile,
               Receiver_ID: res.data.Receiver_ID,
            })

            lat.latitude = res.data.Biz_PrimaryLat
            lat.longitude = res.data.Biz_PrimaryLng
            _this.getEnterpriseLoc(lat)
         }
      })
   },

   //选择所在位置
   selectAddress(e) {
      wx.chooseLocation({
         success(res) {
            console.log("【允许授权】===", res)
            map.getLocationName(res, function (res2) {
               //console.log('res2======',res2)
               var str = res2.location.result
               _this.setData({
                  detail_info: str.address_component.street_number,
                  wd: str.ad_info.location.lat,
                  jd: str.ad_info.location.lng,
                  inprovince: str.address_component.province,
                  incity: str.address_component.city,
                  indistrict: str.address_component.district
               })
               _this.getPuaCode()
            })
         },
         fail: function (res) {
            if (res.errMsg.indexOf('auth') != -1) {
               wx.openSetting({
                  success: function (res) {
                     wx.setStorageSync("noMap", 1)
                  }
               })
            }
         }
      })
   },

   //通过经纬度获取中文地址
   getEnterpriseLoc: function (loc) {
      map.getLocationName(loc, function (res) {
         var str = res.location.result
         //console.log('res======', res)
         _this.setData({
            detail_info: str.address_component.street_number,
            wd: str.ad_info.location.lat,
            jd: str.ad_info.location.lng,
            inprovince: str.address_component.province,
            incity: str.address_component.city,
            indistrict: str.address_component.district
         })
         _this.getPuaCode()
      })
   },
   //提交修改
   changeEnterpriseAddress: function (e) {
      //console.log('bizDetail===', _data.bizDetail)
      var info = e.detail.value
      var bizDetail = _data.bizDetail
      var telRule = /^1[0-9]{10}$/
      //console.log('info==', info)
      var url = 'jyhs/Admin/saveInfo'
      if (info.Receiver_Name == '') {
         this.showMessage('请输入商家名称')
      } else if (info.Address_Mobile == '') {
         this.showMessage('请输入手机号码')
      } else if (!telRule.test(info.Address_Mobile)) {
         this.showMessage('手机号码格式不正确')
      } else if (info.Address_Province == '') {
         this.showMessage('请选择所在省')
      } else if (info.Address_City == '') {
         this.showMessage('请选择所在市')
      } else if (info.Address_Area == '' && this.info.districtCode.length != 0) {
         this.showMessage('请选择所在地区')
      } else if (info.Address_Detailed == '') {
         this.showMessage('请输入详细地址')
      }

      var param = {
         Biz_ID: bizDetail.Biz_ID,
         Biz_Phone: info.Address_Mobile,
         Biz_Contact: info.Receiver_Name,
         Biz_PrimaryLng: _data.jd,
         Biz_PrimaryLat: _data.wd,
         Biz_Province: _data.addressCode.PID,
         Biz_City: _data.addressCode.CID,
         Biz_Area: _data.addressCode.AID,
         Biz_Detail_Address: info.Address_Detailed,
         Receiver_ID: _data.Receiver_ID,

      }
      //console.log('param', param)
      co.getShareApi(url, param, function (res) {
         //console.log('res===',res)
         if (res.code == 0) {

            _this.showMessage(' 修改成功')
            setTimeout(function () {
               var pages = getCurrentPages(),
                  prevPage = pages[pages.length - 2];
               prevPage.setData({
                  alter: "123"
               })
               wx.navigateBack({})
            }, 2000)
         }
      })
   },

   //显示提示信息
   showMessage: function (text) {
      var that = this
      that.setData({
         showMessage: true,
         messageContent: text
      })
      setTimeout(function () {
         that.setData({
            showMessage: true,
            messageContent: text
         })
         setTimeout(function () {
            that.setData({
               showMessage: false,
               messageContent: ''
            })
         }, 3000)
      })
   },
   //获取省市区编号
   getPuaCode: function () {
      var url = 'api/Areas/getPCACode'
      var param = {
         Province_Name: _data.inprovince,
         City_Name: _data.incity,
         Area_Name: _data.indistrict
      }
      co.getShareApi(url, param, function (res) {
         if (res.code == 0) {
            _this.setData({
               addressCode: res.data
            })
         }
      })
   },
   //关闭弹出
   closePop: function () {
      this.setData({
         workerPop: false
      })
   },
   //全部列表
   allReserve: function () {
      this.setData({
         workerPop: true
      })
   },
   //获取该企业的员工列表
   getWorkerList: function () {
      var url = 'jyhs/Usermg/getWorkerList'
      co.getShareApi(url, {
         Biz_ID: _opt.Biz_ID
      }, function (res) {
         console.log('res', res)
         if (res.code == 0) {
            _this.setData({
               workerList: res.data
            })
         }
      })
   },
   //选择联系人
   selectReceiver: function (e) {
      console.log(e)
      var selectReceiver_id = e.currentTarget.dataset.receiver_id
      var selectName = e.currentTarget.dataset.name
      var selectPhone = e.currentTarget.dataset.phone

      this.setData({
         reserveName: selectName,
         reservePhone: selectPhone,
         Receiver_ID: selectReceiver_id
      })
      _this.closePop()

   }
})